package org.oneblog.controller;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.oneblog.data.util.QrCodeTools;
import org.oneblog.service.IImageService;
import org.oneblog.util.ImageUtils;
import org.oneblog.util.SizeBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller("StaticController")
@RequestMapping("/staticsrc/")
public class StaticController {

    @RequestMapping(value = "/img/{ym}/{dh}/{file}.jpg{size}")
    public void showPic(HttpServletRequest request, HttpServletResponse response
            , @PathVariable String ym
            , @PathVariable String dh
            , @PathVariable String file
            , @PathVariable String size
            ) {
        
        try {
            // 读图片
            FileInputStream inputStream = imageService.readImageByRelativePath(ym + "/" + dh + "/" + file + ".jpg");
            SizeBean sizeBean = new SizeBean(size);
            if(sizeBean.isSizeValidate()) {

                OutputStream stream = new BufferedOutputStream(response.getOutputStream());
                ImageUtils.scaleWithWidth(inputStream, stream, sizeBean.getWidth());

                stream.flush();
                stream.close();
            } else {
                int available = inputStream.available();
                byte[] data = new byte[available];
                inputStream.read(data);
                inputStream.close();
                // 写图片
                response.setContentType("image/jpg");
                response.setCharacterEncoding("UTF-8");
                OutputStream stream = new BufferedOutputStream(response.getOutputStream());
                stream.write(data);
                stream.flush();
                stream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    @RequestMapping(value = "/qrCode")
    public void testQrCode(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam(value = "url", defaultValue = "", required = false) String url
            ) {
        
        try {
            // 写图片
            response.setContentType("image/jpg");
            response.setCharacterEncoding("UTF-8");
            OutputStream stream = new BufferedOutputStream(response.getOutputStream());
            if(StringUtils.isBlank(url)) {
            	url = "https://www.oomabc.com";
            }
            stream.write(QrCodeTools.creatRrCode(url, 300, 300));
            stream.flush();
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Resource(name = "imageService")
    private IImageService imageService;
}
